package com.iris.live.services.common;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ThinkPad on 2016/11/10.
 */
public class JsonObjectUtils {
//    private static Logger logger = LoggerFactory.getLogger(JsonObjectUtils.class);

    /**
     * @param type      审批类型
     * @param result    审批状态
     * @param quoteid   报价编号/车架号
     * @param custphone 顾客电话
     * @param custid    顾客编号（不是email和姓名）
     * @param sccode    销售顾问（email）
     * @return
     */
    public static JsonObject getJsonObject(String type, String result, String quoteid,
                                           String custphone, String custid, String sccode) {
        JsonObject json = new JsonObject();
        json.addProperty("type", type);
        json.addProperty("result", result);
        json.addProperty("quoteid", quoteid);
        json.addProperty("custphone", custphone);
        json.addProperty("custid", custid);
        json.addProperty("sccode", sccode);
        return json;
    }

    /**
     * @param type      审批类型
     * @param result    审批状态
     * @param quoteid   报价编号/车架号
     * @param custphone 顾客电话
     * @param custid    顾客编号（不是email和姓名）
     * @param sccode    销售顾问（email）
     * @param initQuotationNumber    初始报价单号
     * @return
     */
    public static JsonObject getJsonObject(String type, String result, String quoteid,
                                           String custphone, String custid, String sccode,String initQuotationNumber) {
        JsonObject json = new JsonObject();
        json.addProperty("type", type);
        json.addProperty("result", result);
        json.addProperty("quoteid", quoteid);
        json.addProperty("custphone", custphone);
        json.addProperty("custid", custid);
        json.addProperty("sccode", sccode);
        json.addProperty("initQuotationNumber",initQuotationNumber);
        return json;
    }

    public static String writeAsJson(Object obj){
        if(obj == null) return "";
        Gson gson = new Gson();
        return gson.toJson(obj);
    }

    public static String createJsonString(Object value) {
        ObjectMapper mapper = new ObjectMapper();
        String json = "";
        try {
            json = mapper.writeValueAsString(value);
        } catch (Exception e) {
//            logger.error("createJsonString出错",e);
        }
        return json;
    }

    public static List getList(String json, Class<?> collectionClass, Class<?>... elementClasses) {
            ObjectMapper mapper = new ObjectMapper();
            JavaType javaType = getCollectionType(mapper,collectionClass,elementClasses);
            List list = new ArrayList();
            try {
                list = mapper.readValue(json,javaType);
            } catch (Exception e){
//                logger.error("getList出错",e);
            }
            return list;
    }

    public static JavaType getCollectionType(ObjectMapper mapper, Class<?> collectionClass, Class<?>... elementClasses) {
        return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    public static void main(String[] args){
        System.out.println(writeAsJson("wwwwww"));
    }
}
